package com.neusoft.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neusoft.crm.entity.CommonResult;
import com.neusoft.crm.entity.Dept;
import com.neusoft.crm.mapper.DeptMapper;
import com.neusoft.crm.service.IDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 东方红
 * @since 2023-03-22
 */
@Service
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements IDeptService {
    @Autowired
    DeptMapper deptMapper;

    @Override
    public CommonResult getAll() {

        QueryWrapper<Dept> wrapper = new QueryWrapper();
        wrapper.eq("nbl" ,"1");

        List<Dept>  lds = deptMapper.selectList(wrapper);
        CommonResult< List<Dept> > result =  new CommonResult(lds);
        if (lds ==null || lds.size()==0){
            result.setNoutFound();
        }
        return  result;
    }

    /**
     * 保存新建部门
     *
     * @param dept
     * @return
     */
    @Override
    public CommonResult addDept(Dept dept) {
        /**
         * 必须情况，否则，数据库不使用自动标识列的值
         */
        dept.setDeptno(null);
        int i = deptMapper.insert(dept);
        CommonResult result =new CommonResult(dept.getDeptno());
        if (i <1){
            result.setNotModified();
        }
        return result;
    }

    /**
     * 修改部门信息
     *
     * @param dept
     * @return
     */
    @Override
    public CommonResult modDept(Dept dept) {
        int i =  deptMapper.updateById(dept);
        CommonResult  result = new CommonResult(i);
        if (i <1){
            result.setNotModified();
        }
        return result;
    }

    @Override
    public CommonResult delDept(String deptno) {
        int i = deptMapper.deleteById(deptno);
        CommonResult  result = new CommonResult(i);
        if (i <1){
            result.setNotDeleted();
        }
        return result;
    }


}
